<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'AIR2_CRUD_Controller.php';

/**
 * Submission Controller
 *
 * @author rcavis
 * @package default
 */
class Submission_Controller extends AIR2_CRUD_Controller {

    public $base_model_name = 'SrcResponseSet';
    public $base_view_name  = 'submission';
    public $exposed_relations = array(
        'annotations' => 'SrsAnnotation',
        'responses' => 'SrcResponse',
        'source' => 'Source',
    );
    public $taggable = true;


    /**
     * Add custom relations to queries to this controller
     *
     * @param AIR2_Query $q
     * @param boolean $deeply (optional)
     */
    protected function _add_query_load_related(&$q, $deeply=true) {
        if (!$this->rel_uri) {
            $q->leftJoin('a.Inquiry i');
            $q->leftJoin('i.SrcInquiry si where si.src_id = a.srs_src_id');
            $q->leftJoin('i.CreUser icu');
            $q->leftJoin('icu.UserOrg icuo WITH icuo.uo_home_flag = true');
            $q->leftJoin('icuo.Organization');
        }
        elseif ($this->rel_uri === 'annotations') {
            $q->leftJoin('a.CreUser c');
            $q->leftJoin('c.UserOrg uo WITH uo.uo_home_flag = true');
            $q->leftJoin('uo.Organization o');
        }
        elseif ($this->rel_uri === 'responses') {
            $count = 'select count(*) from sr_annotation where sran_sr_id = a.sr_id';
            $q->select("a.*, q.*, ($count) as annot_count");
            $q->leftJoin('a.Question q');
            //$q->leftJoin('a.SrAnnotation sa');
            $this->extra_field_defs []= array('name' => 'annot_count', 'type' => 'int');
        }
        elseif ($this->rel_uri === 'source') {
            $q->leftJoin('a.SrcEmail se WITH se.sem_primary_flag = true');
            $q->leftJoin('a.SrcPhoneNumber sp WITH sp.sph_primary_flag = true');
            $q->leftJoin('a.SrcMailAddress sm WITH sm.smadd_primary_flag = true');
            $q->leftJoin('a.SrcVita sv WITH sv.sv_type = ?', SrcVita::$TYPE_EXPERIENCE);
            $q->leftJoin('a.SrcFact sf');
            $q->leftJoin('sf.Fact sff');
            $q->leftJoin('sf.AnalystFV sfav');
            $q->leftJoin('sf.SourceFV sfsv');
        }
        parent::_add_query_load_related($q, $deeply);
    }


    /**
     * Custom data and views for html requests
     *
     * @param associative-array $data
     * @param string  $view_name
     * @return string html body
     */
    public function get_html_body($data, $view_name=null) {
        if (!$this->base_uuid) { // redirect to search
            redirect('todosomething');
        }
        elseif ($this->base_uuid && !$this->rel_uri) {
            $data = $this->_html_dashboard($data);
        }
        return parent::get_html_body($data, $view_name);
    }


    /**
     * Set the page title
     *
     * @param array   $head
     * @return array
     */
    public function get_html_head($head=array()) {
        if ($this->base_uuid) {
            $src = $this->_find_base()->Source;
            $src_name = $src->src_username;
            if ($src->src_first_name && $src->src_last_name) {
                $src_name = $src->src_first_name.' '.$src->src_last_name;
            }
            $inq_name = $this->_find_base()->Inquiry->inq_ext_title;
            $head['title'] = AIR2_SYSTEM_DISP_NAME." - Submission: $src_name - $inq_name";
        }
        return $head;
    }


    /**
     * Get data for HTML dashboard
     *
     * @param associative-array $data
     * @return associative-array
     */
    protected function _html_dashboard($data) {
        // annotations
        $q = $this->build_query($this->base_uuid, 'annotations');
        $q->orderBy('a.srsan_upd_dtim DESC');
        $q->limit(6);
        $anno_data = $this->fetch_data($q, true, true);

        // responses
        $q = $this->build_query($this->base_uuid, 'responses');
        $q->orderBy('q.ques_dis_seq ASC');
        $resp_data = $this->fetch_data($q, true, true);

        // source
        $q = $this->build_query($this->base_uuid, 'source');
        $src_data = $this->fetch_data($q, true, false);

        // tags
        $q = $this->airtag->create_tag_query($this->_find_base());
        $q->orderBy('added_dtim ASC');
        $tag_data = $this->airtag->exec_tag_query($q, true, true);

        // submission count
        $conn = AIR2_DBManager::get_connection();
        $inq_id = $this->_find_base()->srs_inq_id;
        $q = 'select count(*) from src_response_set where srs_inq_id = ?';
        $subm_count = $conn->fetchOne($q, array($inq_id), 0);

        return array(
            'subm_uuid' => $this->base_uuid,
            'subm_url'  => $this->uri_for('submission').'/'.$this->base_uuid,
            'subm_data' => $data,
            'anno_data' => $anno_data,
            'resp_data' => $resp_data,
            'src_data' => $src_data,
            'tag_data' => $tag_data,
            'subm_count' => $subm_count,
        );
    }


}
